package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.product.pojo.dto.ProductSearchDTO;
import com.xbongbong.pro.product.pojo.dto.ProductSerialDTO;
import com.xbongbong.pro.product.pojo.dto.ProductSerialFlowDTO;
import com.xbongbong.pro.product.pojo.vo.ProductSearchVO;
import com.xbongbong.pro.product.pojo.vo.ProductSerialFlowVO;
import com.xbongbong.pro.product.pojo.vo.ProductSerialVO;
import com.xbongbong.pro.productwarehouse.pojo.ItemPoJo;
import com.xbongbong.pro.productwarehouse.pojo.vo.ProductWarehouseSelectListVO;
import com.xbongbong.pro.warehouse.pojo.dto.WarehouseListDTO;
import com.xbongbong.pro.warehouse.pojo.vo.WarehouseListVO;
import com.xbongbong.saas.domain.entity.ext.WarehouseEntityExt;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.saas.service.ProductSerialService;
import com.xbongbong.saas.service.ProductService;
import com.xbongbong.saas.service.WarehouseService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 产品
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/8/13 14:01
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/product")
public class MobileProductController {
    @Resource
    private ProductService productService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private ProductSerialService productSerialService;

    /**
     * 产品查询
     *
     * @param productSearchDTO 产品查询入参
     * @param br 处理错误
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/8/13 14:03
     * @since v1.0
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String search(@RequestBody @Valid ProductSearchDTO productSearchDTO, BindingResult br) {
        XbbResponse<ProductSearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSearchVO productSearchVO = productService.productSearch(productSearchDTO);
                response = new XbbResponse<>(productSearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/warehouse/select", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String warehouseSelect(@RequestBody @Valid WarehouseListDTO warehouseListDTO, BindingResult br) {
        XbbResponse<ProductWarehouseSelectListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            WarehouseListVO warehouseListVO = warehouseService.getManagetWarehouses(warehouseListDTO);
            List<WarehouseEntityExt> allWarehouseList = warehouseListVO.getWarehouseList();
            if (allWarehouseList == null) {
                allWarehouseList = new ArrayList<>();
            }
            List<ItemPoJo> warehouseList = new ArrayList<>();
            for (WarehouseEntityExt warehouseEntityExt : allWarehouseList) {
                JSONObject dataObj = warehouseEntityExt.getData();
                if (dataObj != null) {
                    String name = dataObj.getString(WarehouseEnum.WAREHOUSE_NAME.getAttr());
                    Long id = warehouseEntityExt.getId();
                    ItemPoJo itemPoJo = new ItemPoJo(id, name);
                    warehouseList.add(itemPoJo);
                }
            }
            ProductWarehouseSelectListVO selectListVO = new ProductWarehouseSelectListVO();
            selectListVO.setWarehouseList(warehouseList);
            response = new XbbResponse<>(selectListVO);
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取序列号
     * @param productSerialDTO 入参
     * @param br 参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/getSerial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerial(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.getSerial(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取序列号流水
     * @param productSerialFlowDTO 入参
     * @param br 数据绑定
     * @return 流水记录
     */
    @RequestMapping(value = "/getSerialFlow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerialFlow(@RequestBody @Valid ProductSerialFlowDTO productSerialFlowDTO, BindingResult br){
        XbbResponse<ProductSerialFlowVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialFlowVO productSerialFlowVO = productSerialService.getSerialFlow(productSerialFlowDTO);
                response = new XbbResponse<>(productSerialFlowVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 出库批量录入判断序列号是否在库
     * @param productSerialDTO 入参
     * @param br 参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/checkSerialStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkSerialStatus(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.checkSerialStatus(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
